/**
 * Copyright (c) 2013-5-2 下午3:50:29 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.dao.music;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extblog.wkm.web.dao.AbstractDao;
import org.extblog.wkm.web.util.PropertyUtil;
import org.extblog.wkm.web.vo.MusicFormView;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;

/**
 * @author WKM
 * Service.java history
 * 1.WKM - 2013-5-2 - Create File
 */
@Controller
public class MusicDao extends AbstractDao {


	/**
	 * 添加 body
	 * @param view
	 * @return
	 */
	public int insertMusic(MusicFormView view){
		log.info("insert:" + PropertyUtil.getValue("music.insert"));
		return getTemplate().update(PropertyUtil.getValue("music.insert"),
					view.getMusicId(),
					view.getTitle(),
					view.getSinger(),
					view.getAddress(),
					view.getLyrics());
	}
	
	public List<Map<String, String>> selectMusics(){
		log.info("query:" + PropertyUtil.getValue("music.select"));
		return getTemplate().query(PropertyUtil.getValue("music.select"), new Mapper());
	}
	
	public List<Map<String, String>> selectMusicById(String musicId){
		log.info("query:" + PropertyUtil.getValue("music.select.byid"));
		return getTemplate().query(PropertyUtil.getValue("music.select.byid"), new Mapper(),musicId);
	}
	
	public int updateBody(MusicFormView view){
		log.info("update:" + PropertyUtil.getValue("music.update"));
		return getTemplate().update(PropertyUtil.getValue("music.update"),
				view.getTitle(),
				view.getSinger(),
				view.getAddress(),
				view.getLyrics(),
				view.getMusicId());
	}
	
	public int deleteBody(MusicFormView view){
		log.info("delete:" + PropertyUtil.getValue("music.delete"));
		return getTemplate().update(PropertyUtil.getValue("music.delete"),view.getMusicId());
	}
	
	
	/**
	 * 数据单条记录使用map记录
	 * @author WKM
	 * FaceDao.java history
	 * 1.WKM - Jan 16, 2013 - Create File
	 */
	private class Mapper implements RowMapper<Map<String, String>>{

		public Map<String, String> mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			// TODO Auto-generated method stub
			Map<String, String> map = new HashMap<String, String>();
			String musicId = rs.getString("music_id");
			if(musicId != null){
				map.put("musicId", musicId);
			}
			
			String title = rs.getString("music_title");
			if(title != null){
				map.put("title", title);
			}
			
			String singer = rs.getString("music_singer");
			if(singer != null){
				map.put("singer", singer);
			}
			
			String address = rs.getString("music_address");
			if(address != null){
				map.put("address", address);
			}
			String lyrics = rs.getString("music_lyrics");
			if(lyrics != null){
				map.put("lyrics", lyrics);
			}
			
			return map;
		}
	}
}
